﻿using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMainTask.IMainTaskParsers;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.MainTasks;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.EdgeInfos;
using HamcoWcs.Service.Common;

namespace HamcoWcs.Service.MainTasks.SubTaskParsers
{
    [AutoInject(typeof(ISubTaskParser), InjectType.Scope)]
    public class SubTaskParser_SrmOutTask : SubTaskParserBase
    {
        public override EnumSubTaskType SubTaskType => EnumSubTaskType.SrmOutTask;

        private readonly WcsDbContext _wcsDbContext;
        public SubTaskParser_SrmOutTask(WcsDbContext wcsDbContext, ILocationService locationService) : base(locationService)
        {
            _wcsDbContext = wcsDbContext;
        }

        public override void Deal(MainTask mainTask, EdgeInfoDto edge)
        {


            //var extend = JsonConvert.DeserializeObject<ExtendDto>(mainTask.Extend);
            //if (extend == null) throw new Exception($"未获取到任务[{mainTask.TaskId}]的扩展信息");

            //var floc = _locationService.GetPalletLocation(mainTask.UnitId) ??
            //    throw new WcsRunException(mainTask.UnitId, $"未在货架中找到对应栈板[{mainTask.UnitId}]");


            var loc = _locationService.GetPalletLocation(mainTask.UnitId);
            loc.LocStatus = (int)EnumLocStatus.Out;
            if (string.IsNullOrEmpty(mainTask.SrcPosition))
            {
                mainTask.SrcPosition = loc.WcsLocName;
            }
            _wcsDbContext.SaveChanges();

            var subTask = CreateNewSubTask(mainTask, mainTask.SrcPosition, edge.ToNode, edge.ExcutePlc);
            mainTask.SubTasks.Add(subTask);


        }

    }
}
